package com.baor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.CustomerDao;
import com.baor.dao.Removal_recordDao;
import com.baor.dao.UserDao;
import com.baor.domain.*;
import com.baor.service.RemovalService;
import com.baor.util.R;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class RemovalServiceImpl implements RemovalService {
    /**
     * 清运记录
     */

    @Autowired
    private Removal_recordDao removalRecordDao;

    @Autowired
    private UserDao userDao;

    @Override
    public R getList(Map<String, Object> params) {
        Page page = new Page();
        page.setCurrent((Integer) params.get("pageIndex"));
        page.setSize((Integer) params.get("pageSize"));

        MPJLambdaWrapper<RemovalRecordVO> wrapper = new MPJLambdaWrapper<>(RemovalRecordVO.class)
                .select(RemovalRecordVO::getEquipmentId,
                        RemovalRecordVO::getWorkerId,
                        RemovalRecordVO::getWeight,
                        RemovalRecordVO::getRemovalTime)
                .select(UserVO::getId,
                        UserVO::getUsername,
                        UserVO::getPhone)
                .select(Equipment::getCode,
                        Equipment::getAddress)
                .leftJoin(Equipment.class, Equipment::getId, RemovalRecordVO::getEquipmentId)
                .leftJoin(UserVO.class, UserVO::getId, RemovalRecordVO::getWorkerId)
                .orderByDesc(RemovalRecordVO::getRemovalTime);


        String code = (String) params.get("code");
        String name = (String) params.get("name");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        // 添加设备编号
        if (StringUtils.hasText(code)) {
            wrapper.like(Equipment::getCode, code);
        }

        if (StringUtils.hasText(name)) {
            wrapper.like(CustomerVO::getRealname, name);
        }

        // 开始时间
        if (StringUtils.hasText(startTime)) {
            // 大于等于开始时间
            wrapper.ge(RemovalRecordVO::getRemovalTime, startTime);
        }
        //结束时间
        if (StringUtils.hasText(endTime)) {
            // 小于等于结束时间
            wrapper.le(RemovalRecordVO::getRemovalTime, endTime);
        }
        Page<RemovalVO> pageList = removalRecordDao.selectJoinPage(page, RemovalVO.class, wrapper);
        Map map = new HashMap();
        map.put("totalSize", pageList.getTotal());
        map.put("List", pageList.getRecords());
        return R.ok(map);
    }

    @Override
    public R getCleaner(Map<String, Object> params) {
        Page page = new Page();
        page.setCurrent((Integer) params.get("pageIndex"));
        page.setSize((Integer) params.get("pageSize"));
        MPJLambdaWrapper<UserVO> wrapper = new MPJLambdaWrapper<>(UserVO.class)
                .select(UserVO::getId,
                        UserVO::getUsername)
                .leftJoin(UserRoleVo.class, UserRoleVo::getUserId, UserVO::getId)
                .eq(UserRoleVo::getRoleId, 4);
        Page<CustomerNameVO> pageList = userDao.selectJoinPage(page, UserVO.class, wrapper);
        Map map = new HashMap();
        map.put("totalSize", pageList.getTotal());
        map.put("List", pageList.getRecords());
        return R.ok(map) ;
    }
}
